<template>
  <view class="instrument">
    <scroll-view @scrolltolower="scrolltolower" refresher-background="rgba(0, 0, 0, 0)" class="scrollView" refresher-enabled="true" :refresher-triggered="triggered" :refresher-threshold="100" @refresherrefresh="onRefresh" @refresherrestore="onRestore" :style="{'height':  'calc('+ screenHeight  +'px' +' ' + '-' +' ' + excessHeight +'rpx)'}" scroll-y="true">
      <view class="box" v-for="(item,index) in list" :key="index" @click.stop="toDetail(item)">
        <view class="demo-avatar">
          <view class="pic">
            <image class="point-tag" :src="item.avatar"></image>
          </view>
          <view class="info-box">
            <view class="info1"> {{item.nickname}}</view>
            <view class="info2">
              <view class="tag v" v-if="item.invite_vip==1">Vip </view>
              <view class="tag j" v-if="item.institution_vip_jy==1">教育机构 </view>
              <view class="tag y" v-if="item.institution_vip_gys==1">乐器供应商 </view>
              <view class="tag z" v-if="item.manufacturer_vip==1">制造商 </view>
            </view>
            <view class="info3">
              粉丝：{{item.fan_num}} ｜ 关注：{{item.follow_num}}
            </view>
          </view>
          <view class="status" @click.stop="showWin(item,index)">
            <image class="status-tag" v-if="item.relation_status==1" src="/static/images/indexpage/guanzhu.png"></image>
            <image class="status-tag" v-else-if="item.relation_status==2" src="/static/images/indexpage/yiguan.png"></image>
            <image class="status-tag" v-else-if="item.relation_status==3" src="/static/images/indexpage/huguan.png"></image>
            <image class="status-tag" v-else-if="item.relation_status==4" src="/static/images/indexpage/yichu.png"></image>

          </view>
        </view>
      </view>
      <view class="nodata-st" v-if="form.page==form.last_page"> ------没有更多数据了------</view>
      <view class="nodata-st" v-if="list.length ==0"> ------暂无数据------</view>
      <view style="height:24rpx;"></view>
    </scroll-view>

    <x-modal v-model="contractObj.isShow" :showBotton="contractObj.showBotton" :showOneBotton="contractObj.showOneBotton" :title="contractObj.title" @confirm="confirm" @cancel="cancel" :cancelText="contractObj.cancelText" :confirmText="contractObj.confirmText" :canColse="false">
      {{contractObj.content}}
    </x-modal>
  </view>
</template>

<script>

import api from '@/api/index.js';
export default {
  name: "x-instrument",
  props: {
    screenHeight: {
      type: Number,
      default: uni.getStorageSync('screenHeight') - uni.getStorageSync('safeAreaBottom')
    },
    excessHeight: {
      type: Number,
      default: 0
    },
    query: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      contractObj: {
        title: '提示',
        content: '是否关注该用户?',// content: '是否对该用户取消关注?',//content: '是否将该用户移出黑名单?',
        isShow: false,
        showBotton: true,
        cancelText: '否',
        confirmText: '是',
        showOneBotton: false
      },
      triggered: true,
      list: [],
      form: {
        page: 1,
        page_size: 10,
        last_page: 0,
      },
      userStatus: '',
      user_id: '',
      user_index: ''
    };
  },
  mounted() {

    console.log(this.query)
    this._freshing = false;
    this.init()
    setTimeout(() => {
      this.triggered = true;
    }, 1000)

  },
  methods: {
    init() {
      this.form.page = 1;
      this.form.last_page = 0;
      //黑名单
      if (this.query.position == 9) {
        this.getblackList()
      } else {
        this.getpage()
      }
    },
    getblackList() {
      api.base.getblackList({ ...this.form, ...this.query }).then(res => {
        if (res.code == 1) {
          this.list = this.form.page === 1 ? res.data.data : this.list.concat(res.data.data);
          this.form.last_page = res.data.last_page;
        } else {
          uni.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
      });
    },
    getpage() {
      api.base.getuserList({ ...this.form, ...this.query }).then(res => {
        if (res.code == 1) {
          this.list = this.form.page === 1 ? res.data.data : this.list.concat(res.data.data);
          this.form.last_page = res.data.last_page;
        } else {
          uni.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
      });
    },
    onRefresh() {
      if (this._freshing) return;
      this._freshing = true;
      this.init()
      setTimeout(() => {

        this.triggered = false;
        this._freshing = false;
      }, 1000)
    },
    onRestore() {
      this.triggered = 'restore'; // 需要重置
      console.log("onRestore");
    },
    scrolltolower() {
      if (this.form.page < this.form.last_page) {
        this.form.page += 1;
        //黑名单
        if (this.query.position == 9) {
          this.getblackList()
        } else {
          this.getpage()
        }
      }
    },
    toDetail(item) {
      uni.navigateTo({
        url: `/pages/otherPeople/index?user_id=` + item.user_id
      });
    },
    cancel(e) {
      this.contractObj.isShow = false
    },
    confirm(e) { // third_uid   getUserdelete
      let that = this
      if (that.userStatus == 4) {
        api.base.getUserdelete({ user_id: that.user_id }).then(res => {
          if (res.code == 1) {
            that.list.splice(that.user_index, 1)
            if (that.list.length == 0) {
              that.form.last_page = 0
            }

            uni.$u.toast(res.msg)
            that.contractObj.isShow = false
          } else {
            uni.showToast({
              title: res.msg,
              icon: 'none'
            });
          }
        });
      } else {
        api.base.getTrendsFollow({ third_uid: that.user_id }).then(res => {
          if (res.code == 1) {
            that.list[that.user_index].relation_status = res.data.status
            uni.$u.toast(res.msg)
            that.contractObj.isShow = false
          } else {
            uni.showToast({
              title: res.msg,
              icon: 'none'
            });
          }
        });
      }



    },
    showWin(item, index) {
      let that = this

      that.userStatus = item.relation_status
      that.user_id = item.user_id ? item.user_id : item.id
      that.user_index = index
      // content: '是否对该用户取消关注?',//content: '是否将该用户移出黑名单?',
      if (item.relation_status == 1) {
        that.contractObj.content = '是否关注该用户?'
      } else if (item.relation_status == 2 || item.relation_status == 3) {
        that.contractObj.content = '是否对该用户取消关注?'
      } else if (item.relation_status == 4) {
        that.contractObj.content = '是否将该用户移出黑名单?'
      }
      that.contractObj.isShow = true
    }
  }
}
</script>

<style lang="scss" scoped>
.instrument {
  .box {
    margin: 0rpx 32rpx 32rpx 32rpx;
    width: calc(100% - 64rpx);
    height: 190rpx;
    background: #ffffff;
    border-radius: 20rpx 20rpx 20rpx 20rpx;
    .demo-avatar {
      height: 190rpx;
      padding-left: 30rpx;
      display: flex;
      position: relative;
      .pic {
        display: flex;
        justify-content: center;
        align-items: center;
        float: left;
        width: 96rpx;
        height: 190rpx;
        .point-tag {
          width: 96rpx;
          height: 96rpx;
          border-radius: 7694rpx 7694rpx 7694rpx 7694rpx;
        }
      }
      .info-box {
        height: 126rpx;
        margin: 32rpx;
        .info1 {
          width: 250rpx;
          height: 44rpx;
          font-size: 32rpx;
          font-family: PingFang SC-Bold, PingFang SC;
          font-weight: bold;
          color: #333333;
          line-height: 44rpx;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 1;
        }
        .info2 {
          display: flex;
          height: 32rpx;
          margin-top: 10rpx;
          margin-bottom: 10rpx;
          .tag {
            display: flex;
            align-items: center;
            padding: 2rpx 8rpx;
            border-radius: 8rpx 8rpx 8rpx 8rpx;
            font-size: 20rpx;
            font-family: PingFang SC-Medium, PingFang SC;
            font-weight: 500;
            margin-right: 12rpx;
          }
          .v {
            color: #ff9018;
            background: rgba(255, 144, 24, 0.1);
          }
          .j {
            color: #58bf90;
            background: rgba(35, 165, 102, 0.1);
          }
          .z {
            color: #5875bf;
            background: rgba(88, 117, 191, 0.1);
          }
          .y {
            color: #bf5858;
            background: rgba(191, 88, 88, 0.1);
          }
        }
        .info3 {
          height: 34rpx;
          font-size: 24rpx;
          font-family: PingFang SC-Medium, PingFang SC;
          font-weight: 500;
          color: #666666;
        }
      }
      .status {
        position: absolute;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 190rpx;
        right: 30rpx;
        .status-tag {
          width: 120rpx;
          height: 50rpx;
        }
      }
    }
  }
}
</style>